//
//  LanguageViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2018/1/3.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "LanguageViewController.h"
#import "LanguageTableViewCell.h"
@interface LanguageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)LanguageTableViewCell *cell;

@end

@implementation LanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"语言");
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 44)];
    [btn setTitle:MyString(@"保存") forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:12];
    [btn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:item];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}
-(void)saveClick{
    LanguageTool *tool=[LanguageTool sharedInstance];
    NSIndexPath*path=[self.tableView indexPathForCell:self.cell];
    if (path.row==0) {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        
        NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"];
        
        NSString * preferredLang = [allLanguages objectAtIndex:0];
        if ([preferredLang isEqualToString:@"zh-Hans-CN"]) {
            [tool changeNowLanguage:CNS];
        }else if ([preferredLang isEqualToString:@"bo-CN"]){
            [tool changeNowLanguage:EN];
        }else {
            NSLog(@"当前语言:%@", preferredLang);
        }
    }else if (path.row==1){
        [tool changeNowLanguage:CNS];
    }else{
        [tool changeNowLanguage:EN];
    }
    
    NSLog(@"baocun");
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorInset=UIEdgeInsetsZero;
        _tableView.separatorColor=SeparatorColor;
        _tableView.tableFooterView=[UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"LanguageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LanguageTableViewCell"];
    }
    return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"LanguageTableViewCell";
    LanguageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:LANGUAGE_DEFAULT];

    if (indexPath.row==0) {
        cell.lanLabel.text=@"系统默认";
        if ([tmp isEqualToString:@"Default"]||tmp==nil) {
            cell.selected=YES;
            self.cell=cell;
        }
    }else if (indexPath.row==1){
        cell.lanLabel.text=@"中文";
        if ([tmp isEqualToString:CNS]) {
            cell.selected=YES;
            self.cell=cell;
        }
    }else{
        cell.lanLabel.text=@"藏文";
        if ([tmp isEqualToString:EN]) {
            cell.selected=YES;
            self.cell=cell;
        }
    }
    cell.selectBtn.selected=cell.selected;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LanguageTableViewCell*cell=[tableView cellForRowAtIndexPath:indexPath];
    cell.selectBtn.selected=cell.selected;
    if (self.cell!=cell) {
        self.cell.selectBtn.selected=self.cell.selected;
        self.cell=cell;
    }
    if ([self.tableView indexPathForCell:self.cell].row==0) {
        [[NSUserDefaults standardUserDefaults]setObject:DEFAULT forKey:LANGUAGE_DEFAULT];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else if ([self.tableView indexPathForCell:self.cell].row==1){
        [[NSUserDefaults standardUserDefaults]setObject:CNS forKey:LANGUAGE_DEFAULT];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults]setObject:EN forKey:LANGUAGE_DEFAULT];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
